﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;
using MockPrjTeam2.Pages.Controls;


namespace MockPrjTeam2.Pages.Forms
{
    public partial class TrustDistrictDetails : System.Web.UI.Page
    {
        private int trustDistrictId;
        private TrustDistrictBiz biz;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["id"], out trustDistrictId))
            {
                Response.Redirect("../Forms/TrustRegionDetails.aspx");
            }
            else
            {
                biz = new TrustDistrictBiz();

            }
            if (!IsPostBack)
            {
                GetTrustDistrictById();
            }
            
        }

        

        private void GetTrustDistrictById()
        {

            TrustDistrict trustDistrict = biz.GetTrustDistrictById(trustDistrictId);
            TxtTrustRegionName.Text = trustDistrict.TrustRegionName;
            TxtDescription.Text = trustDistrict.DescriptionDistrict;
            TxtTrustDistrictName.Text = trustDistrict.TrustDistrictName;
            HiddenFieldTrustDistrictId.Value = trustDistrict.TrustDistrictID.ToString();

        }
        public int CheckTrustDistrictNameExist(string trustDistrictName)
        {
            TrustDistrictBiz trustDistrictBiz = new TrustDistrictBiz();
            return trustDistrictBiz.CheckTrustDistrictNameExist(trustDistrictName);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            {
                TrustDistrictBiz biz = new TrustDistrictBiz();
                TrustDistrict trustDistrict = new TrustDistrict();
                int checkTrustDistrictName = CheckTrustDistrictNameExist(TxtTrustDistrictName.Text);
                if (checkTrustDistrictName > 0)
                {
                    lblCheckTrustDistrictName.Text = "Trust District Name does exist ! Please new Trust District Name !";
                    return;
                }
                trustDistrict.TrustDistrictID = trustDistrictId;
                trustDistrict.TrustDistrictName = TxtTrustDistrictName.Text;
                trustDistrict.DescriptionDistrict = TxtDescription.Text;
                trustDistrict.TrustRegionID = trustDistrictId;
                
                
                bool result = biz.UpdateTrustDistrict(trustDistrict);
                if (result == true)
                {
                    Response.Redirect("../Forms/TrustRegionDetails.aspx");
                }
            }
        }

        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            int trustDistrictId;
            if (int.TryParse(HiddenFieldTrustDistrictId.Value, out trustDistrictId))
            {
                TrustDistrictBiz trustDistrictBiz = new TrustDistrictBiz();
                trustDistrictBiz.SetInActive(trustDistrictId, false);
                ButtonInactive.Visible = false;
            }
        }
    }
}